using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Personalization.VisitorGroups;
using EPiServer.PlugIn;
using EPiServer.UI.WebControls;
using EPiServer.Web;
using EPiServer.Web.WebControls;

namespace EPiServer.Labs.MobilePack.Core
{
    [GuiPlugIn(DisplayName = "Redirection", Area = PlugInArea.EditPanel, RequiredAccess = Security.AccessLevel.Administer, Url = "~/modules/MobilePack.Core/RedirectionPlugin.ascx")]
    public partial class RedirectionPlugin : UserControlBase
    {
        protected List<VisitorGroup> VGList { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.Grid.DataSource = RedirectionRule.Instance.LoadForPage(CurrentPage);

            VisitorGroupStore vgs = new VisitorGroupStore();
            VGList = vgs.List().ToList();

            if (!this.IsPostBack)
            {
                this.DataBind();
            }
        }

        protected void AddNewRow(object sender, EventArgs e)
        {
            this.Grid.EditItemIndex = 0;
            ((List<RedirectionRule>)this.Grid.DataSource).Insert(0, new RedirectionRule());
            this.Grid.DataBind();
        }

        protected bool ButtonEnabled(int rowIndex)
        {
            return true;
        }

        protected void CancelEditing(object sender, DataGridCommandEventArgs e)
        {
            this.Grid.DataSource = RedirectionRule.Instance.LoadForPage(CurrentPage);
            this.Grid.EditItemIndex = -1;
            this.Grid.DataBind();
        }

        protected void DeleteRow(object sender, DataGridCommandEventArgs e)
        {
            var dlt = ((List<RedirectionRule>)this.Grid.DataSource)[e.Item.ItemIndex];
            
            RedirectionRule.Instance.DeleteRule(dlt.Id);
            this.Grid.DataSource = RedirectionRule.Instance.LoadForPage(CurrentPage);
            this.Grid.DataBind();
        }

        private PageReference GetPageReference(Guid g)
        {
            var map = PermanentLinkMapStore.Find(g) as PermanentPageLinkMap;
            return (map != null) ? map.PageReference : PageReference.EmptyReference;
        }

        protected string GetPageName(Guid g)
        {
            var pr = this.GetPageReference(g);
            if (pr != PageReference.EmptyReference)
            {
                return DataFactory.Instance.GetPage(this.GetPageReference(g)).PageName;
            }

            return string.Empty;
        }

        protected string GetLanguageIcon(string languageCode)
        {
            if (string.IsNullOrEmpty(languageCode))
            {
                return string.Empty;
            }

            LanguageBranch languageBranch = LanguageBranch.Load(languageCode);

            if (languageBranch == null)
            {
                return string.Empty;
            }

            return string.Format("<img src=\"{0}\" alt=\"{1}\"", languageBranch.SystemIconPath, languageBranch.Name);
        }

        protected void EditRow(object sender, DataGridCommandEventArgs e)
        {
            this.Grid.EditItemIndex = e.Item.ItemIndex;
            this.Grid.DataBind();
        }

        protected void SaveRow(object sender, DataGridCommandEventArgs e)
        {
            var ddl = e.Item.Cells[0].Controls[1] as DropDownList;
            var pr = e.Item.Cells[1].Controls[1] as InputPageReference;
            var languageSelection = e.Item.Cells[2].Controls[1] as LanguageDropDownList;

            if (ddl != null && pr != null && languageSelection != null)
            {
                RedirectionRule.Instance.UpdateRules(this.CurrentPage, ddl.SelectedValue, pr.PageLink, languageSelection.SelectedLanguage);
            }

            this.Grid.EditItemIndex = -1;
            this.Grid.DataSource = RedirectionRule.Instance.LoadForPage(CurrentPage);
            this.Grid.DataBind();
        }
    }
}